home *** CD-ROM | disk | FTP | other *** search
/ User's Choice Windows CD / User's Choice Windows CD (CMS Software)(1993).iso / misc1 / iv26_w30.zip / EXAMPLES / IDRAW / SLSPLINE.C < prev    next >
C/C++ Source or Header  |  1992-01-21  |  8KB  |  256 lines

  1. /*
  2.  * Copyright (c) 1987, 1988, 1989 Stanford University
  3.  *
  4.  * Permission to use, copy, modify, distribute, and sell this software and its
  5.  * documentation for any purpose is hereby granted without fee, provided
  6.  * that the above copyright notice appear in all copies and that both that
  7.  * copyright notice and this permission notice appear in supporting
  8.  * documentation, and that the name of Stanford not be used in advertising or
  9.  * publicity pertaining to distribution of the software without specific,
  10.  * written prior permission.  Stanford makes no representations about
  11.  * the suitability of this software for any purpose.  It is provided "as is"
  12.  * without express or implied warranty.
  13.  *
  14.  * STANFORD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  15.  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
  16.  * IN NO EVENT SHALL STANFORD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  17.  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  18.  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
  19.  * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
  20.  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  21.  */
  22.  
  23. // $Header: slsplines.c,v 1.13 89/10/09 14:49:42 linton Exp $
  24. // implements classes BSplineSelection and ClosedBSplineSelection.
  25.  
  26. #include "isplines.h"
  27. #include "slsplines.h"
  28. #include <InterViews/rubcurve.h>
  29. #include <iostream.h>
  30.  
  31. // BSplineSelection creates its components.
  32.  
  33. BSplineSelection::BSplineSelection (Coord* x, Coord* y, int n, Graphic* gs)
  34. : (gs) {
  35.     Init(x, y, n);
  36. }
  37.  
  38. // BSplineSelection reads data to initialize its graphic state and
  39. // create its components.
  40.  
  41. BSplineSelection::BSplineSelection (istream& from, State* state) : (nil) {
  42.     bspline = nil;
  43.     ReadGS(from, state);
  44.     Coord* x;
  45.     Coord* y;
  46.     int n;
  47.     ReadPoints(from, x, y, n);
  48.     Init(x, y, n);
  49. }
  50.  
  51. // Copy returns a copy of the BSplineSelection.
  52.  
  53. Graphic* BSplineSelection::Copy () {
  54.     Coord* x;
  55.     Coord* y;
  56.     int n = GetOriginal(x, y);
  57.     Graphic* copy = new BSplineSelection(x, y, n, this);
  58.     return copy;
  59. }
  60.  
  61. // GetOriginal returns the control points that were passed to the
  62. // BSplineSelection's constructor.
  63.  
  64. int BSplineSelection::GetOriginal (const Coord*& x, const Coord*& y) {
  65.     return ((BSpline*) bspline)->GetOriginal(x, y);
  66. }
  67.  
  68. // Init creates the graphic's components and stores the arrowheads'
  69. // endpoints.
  70.  
  71. void BSplineSelection::Init (Coord* x, Coord* y, int n) {
  72.     myname = "BSpl";
  73.     ifillbspline = new IFillBSpline(x, y, n);
  74.     bspline = new BSpline(x, y, n);
  75.     lx0 = x[0];
  76.     ly0 = y[0];
  77.     lx1 = x[1];
  78.     ly1 = y[1];
  79.     rx0 = x[n-1];
  80.     ry0 = y[n-1];
  81.     rx1 = x[n-2];
  82.     ry1 = y[n-2];
  83. }
  84.  
  85. // CreateRubberVertex creates and returns the right kind of
  86. // RubberVertex to represent the BSplineSelection's shape.
  87.  
  88. RubberVertex* BSplineSelection::CreateRubberVertex (Coord* x, Coord* y,
  89. int n, int rubpt) {
  90.     return new RubberSpline(nil, nil, x, y, n, rubpt);
  91. }
  92.  
  93. // CreateReshapedCopy creates and returns a reshaped copy of itself
  94. // using the passed points and its graphic state.
  95.  
  96. Selection* BSplineSelection::CreateReshapedCopy (Coord* x, Coord* y, int n) {
  97.     return new BSplineSelection(x, y, n, this);
  98. }
  99.  
  100. // uncacheChildren uncaches the graphic's components' extents.
  101.  
  102. void BSplineSelection::uncacheChildren () {
  103.     if (bspline != nil) {
  104.     uncacheExtentGraphic(ifillbspline);
  105.     uncacheExtentGraphic(bspline);
  106.     }
  107. }
  108.  
  109. // getExtent returns the graphic's extent including a tolerance for
  110. // the arrowheads.
  111.  
  112. void BSplineSelection::getExtent (float& l, float& b, float& cx, float& cy,
  113. float& tol, Graphic* gs) {
  114.     Extent e;
  115.     if (extentCached()) {
  116.     getCachedExtent(e.left, e.bottom, e.cx, e.cy, e.tol);
  117.     } else {
  118.     FullGraphic gstmp;
  119.     concatGSGraphic(ifillbspline, this, gs, &gstmp);
  120.     getExtentGraphic(
  121.             ifillbspline, e.left, e.bottom, e.cx, e.cy, e.tol, &gstmp
  122.         );
  123.     Extent te;
  124.     concatGSGraphic(bspline, this, gs, &gstmp);
  125.     getExtentGraphic(
  126.             bspline, te.left, te.bottom, te.cx, te.cy, te.tol, &gstmp
  127.         );
  128.     e.Merge(te);
  129.     cacheExtent(e.left, e.bottom, e.cx, e.cy, e.tol);
  130.     }
  131.     float right = 2*e.cx - e.left;
  132.     float top = 2*e.cy - e.bottom;
  133.     float dummy1, dummy2;
  134.     transformRect(e.left, e.bottom, right, top, l, b, dummy1, dummy2, gs);
  135.     transform(e.cx, e.cy, cx, cy, gs);
  136.     tol = MergeArrowHeadTol(e.tol, gs);
  137. }
  138.  
  139. // contains returns true if the graphic contains the point.
  140.  
  141. boolean BSplineSelection::contains (PointObj& po, Graphic* gs) {
  142.     BoxObj b;
  143.     getBox(b, gs);
  144.     if (b.Contains(po)) {
  145.     if (containsGraphic(ifillbspline, po, gs)) {
  146.         return true;
  147.     } else if (containsGraphic(bspline, po, gs)) {
  148.         return true;
  149.     } else if (LeftAcont(lx0, ly0, lx1, ly1, po, gs)) {
  150.         return true;
  151.     } else if (RightAcont(rx0, ry0, rx1, ry1, po, gs)) {
  152.         return true;
  153.     }
  154.     }
  155.     return false;
  156. }
  157.  
  158. // intersects returns true if the graphic intersects the box.
  159.  
  160. boolean BSplineSelection::intersects (BoxObj& userb, Graphic* gs) {
  161.     BoxObj b;
  162.     getBox(b, gs);
  163.     if (b.Intersects(userb)) {
  164.     if (intersectsGraphic(ifillbspline, userb, gs)) {
  165.         return true;
  166.     } else if (intersectsGraphic(bspline, userb, gs)) {
  167.         return true;
  168.     } else if (LeftAints(lx0, ly0, lx1, ly1, userb, gs)) {
  169.         return true;
  170.     } else if (RightAints(rx0, ry0, rx1, ry1, userb, gs)) {
  171.         return true;
  172.     }
  173.     }
  174.     return false;
  175. }
  176.  
  177. // draw draws the graphic.
  178.  
  179. void BSplineSelection::draw (Canvas* c, Graphic* gs) {
  180.     drawGraphic(ifillbspline, c, gs);
  181.     drawGraphic(bspline, c, gs);
  182.     drawLeftA(lx0, ly0, lx1, ly1, c, gs);
  183.     drawRightA(rx0, ry0, rx1, ry1, c, gs);
  184. }
  185.  
  186. // drawClipped draws the graphic if it intersects the clipping box.
  187.  
  188. void BSplineSelection::drawClipped (Canvas* c, Coord l, Coord b, Coord r,
  189. Coord t, Graphic* gs) {
  190.     BoxObj box;
  191.     getBox(box, gs);
  192.  
  193.     BoxObj clipBox(l, b, r, t);
  194.     if (clipBox.Intersects(box)) {
  195.     draw(c, gs);
  196.     }
  197. }
  198.  
  199. // ClosedBSplineSelection creates the closed B-spline's filled
  200. // interior and outline.
  201.  
  202. ClosedBSplineSelection::ClosedBSplineSelection (Coord* x, Coord* y, int n,
  203. Graphic* gs) : (gs) {
  204.     myname = "CBSpl";
  205.     Append(new IFillClosedBSpline(x, y, n));
  206.     Append(new ClosedBSpline(x, y, n));
  207. }
  208.  
  209. // ClosedBSplineSelection reads data to initialize its graphic state
  210. // and create the closed B-spline's filled interior and outline.
  211.  
  212. ClosedBSplineSelection::ClosedBSplineSelection (istream& from, State* state)
  213. : (nil) {
  214.     myname = "CBSpl";
  215.     ReadGS(from, state);
  216.     Coord* x;
  217.     Coord* y;
  218.     int n;
  219.     ReadPoints(from, x, y, n);
  220.     Append(new IFillClosedBSpline(x, y, n));
  221.     Append(new ClosedBSpline(x, y, n));
  222. }
  223.  
  224. // Copy returns a copy of the ClosedBSplineSelection.
  225.  
  226. Graphic* ClosedBSplineSelection::Copy () {
  227.     Coord* x;
  228.     Coord* y;
  229.     int n = GetOriginal(x, y);
  230.     Graphic* copy = new ClosedBSplineSelection(x, y, n, this);
  231.     return copy;
  232. }
  233.  
  234. // GetOriginal returns the control points that were passed to the
  235. // ClosedBSplineSelection's constructor.
  236.  
  237. int ClosedBSplineSelection::GetOriginal (const Coord*& x, const Coord*& y) {
  238.     return ((ClosedBSpline*) Last())->GetOriginal(x, y);
  239. }
  240.  
  241. // CreateRubberVertex creates and returns the right kind of
  242. // RubberVertex to represent the ClosedBSplineSelection's shape.
  243.  
  244. RubberVertex* ClosedBSplineSelection::CreateRubberVertex (Coord* x, Coord* y,
  245. int n, int rubpt) {
  246.     return new RubberClosedSpline(nil, nil, x, y, n, rubpt);
  247. }
  248.  
  249. // CreateReshapedCopy creates and returns a reshaped copy of itself
  250. // using the passed points and its graphic state.
  251.  
  252. Selection* ClosedBSplineSelection::CreateReshapedCopy (Coord* x, Coord* y,
  253. int n) {
  254.     return new ClosedBSplineSelection(x, y, n, this);
  255. }
  256.